package itsm.isperp.module.service.configuration;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.module.dto.configuration.ConfCiRelationTypeDTO;
import itsm.isperp.module.entity.configuration.ConfCiRelationType;
import itsm.isperp.module.entity.configuration.ConfCiRelationUsePropety;
import itsm.isperp.module.repository.configuration.ConfCiRelationTypeMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2014-09-22 09:48
 */
@Service
public class ConfCiRelationTypeService extends BaseService<ConfCiRelationType> {

	@Autowired
	protected ConfCiRelationTypeMapper confCiRelationTypeMapper;

	@Autowired
	protected ConfCiRelationUsePropetyService confCiRelationUsePropetyService;

	@Override
	public ConfCiRelationTypeMapper getRepository() {
		return this.confCiRelationTypeMapper;
	}

	public ConfCiRelationType findByCategory(String fromCategoryId,
			String toCategoryId) {
		return this.confCiRelationTypeMapper.findByCategory(fromCategoryId,
				toCategoryId);
	}

	public List<ConfCiRelationType> findByFromCategoryId(String fromCategoryId) {
		return this.confCiRelationTypeMapper
				.findByFromCategoryId(fromCategoryId);
	}

	public List<ConfCiRelationType> findByToCategoryId(String toCategoryId) {
		return this.confCiRelationTypeMapper.findByToCategoryId(toCategoryId);
	}

	public List<ConfCiRelationType> findAllByFromCategoryId(
			String fromCategoryId) {

		ConfCategoryService confCategoryService = ContextHolder
				.getSpringBean("confCategoryService");

		String treePath = confCategoryService.findOne(fromCategoryId)
				.getTreenodePath();

		String[] pathArr = treePath.split("/");
		String str = "";
		for (int i = 1; i < pathArr.length - 1; i++) {
			str += "'" + pathArr[i] + "',";
		}
		str = str.substring(0, str.length() - 1);
		return this.confCiRelationTypeMapper.findAllByFromCategoryId(str);
	}

	public List<ConfCiRelationType> findAllByToCategoryId(String toCategoryId) {
		ConfCategoryService confCategoryService = ContextHolder
				.getSpringBean("confCategoryService");

		String treePath = confCategoryService.findOne(toCategoryId)
				.getTreenodePath();

		String[] pathArr = treePath.split("/");
		String str = "";
		for (int i = 1; i < pathArr.length - 1; i++) {
			str += "'" + pathArr[i] + "";
		}

		return this.confCiRelationTypeMapper.findByToCategoryId(toCategoryId);
	}

	public List<ConfCiRelationTypeDTO> findOneDTO(String id) throws Exception {
		List<ConfCiRelationTypeDTO> result = new ArrayList<>();
		ConfCiRelationType t = this.findOne(id);

		ConfCiRelationTypeDTO dto = new ConfCiRelationTypeDTO(t);
		List<ConfCiRelationUsePropety> ps = this.confCiRelationUsePropetyService
				.findByRelationTypeId(t.getId());
		dto.setProperties(ps);
		result.add(dto);
		return result;
	}

	public List<ConfCiRelationTypeDTO> findAllDTO() throws Exception {
		List<ConfCiRelationTypeDTO> result = new ArrayList<>();
		List<ConfCiRelationType> rs = this.findAll();
		for (ConfCiRelationType t : rs) {

			ConfCiRelationTypeDTO dto = new ConfCiRelationTypeDTO(t);
			List<ConfCiRelationUsePropety> ps = this.confCiRelationUsePropetyService
					.findByRelationTypeId(t.getId());
			dto.setProperties(ps);
			result.add(dto);
		}

		return result;

	}

	public List<ConfCiRelationTypeDTO> findAllDTOByFromCategoyId(
			String fromCategoyId) throws Exception {
		List<ConfCiRelationTypeDTO> result = new ArrayList<>();

		List<ConfCiRelationType> rs = this.confCiRelationTypeMapper
				.findByFromCategoryId(fromCategoyId);
		for (ConfCiRelationType t : rs) {

			ConfCiRelationTypeDTO dto = new ConfCiRelationTypeDTO(t);
			List<ConfCiRelationUsePropety> ps = this.confCiRelationUsePropetyService
					.findByRelationTypeId(t.getId());
			dto.setProperties(ps);
			result.add(dto);
		}

		return result;

	}

	public List<Map<String, Object>> findAllByCategory(String categoryId,
			String categoryParentId) {
		return this.confCiRelationTypeMapper.findAllByCategory(categoryId,
				categoryParentId);
	}
}
